Ext.define('WebOs.ViewFrame.tradition.Tradition', {
	extend : 'WebOs.Kernel.model.ServiceBase',
	app : null,
	configure : null,

	constructor : function(config) {
		Ext.apply(this, config);
		this.callParent(arguments);
	},

	getApplication : function() {
		return this.app;
	},

	tabPanel : null,
	tabManager : null,

	start : function() {
		a = this;
		var me = this;
		Ext.Loader.setPath('tradition', 'webos/viewframework/tradition');
		me.app = new Ext.app.Application({
			name : 'Tradition',

			requires : ['WebOs.ViewFrame.tradition.view.NaviTreePanel',
					'WebOs.ViewFrame.tradition.view.ContentTabPanel',
					'WebOs.ViewFrame.tradition.view.HeaderContainer',
					'WebOs.ViewFrame.tradition.view.ContentPanel',
					'WebOs.ViewFrame.tradition.view.StatusBar',
					'resources.Config', 'WebOs.ex.DefaultPaggingBar',
					'WebOs.ViewFrame.tradition.model.ServiceModel',
					'WebOs.ViewFrame.tradition.model.NavTreeModel',
					'WebOs.ViewFrame.tradition.store.NavTreeStore',
					'WebOs.ViewFrame.tradition.view.TabManager',
					'WebOs.ex.DefaultGridStore', 'WebOs.ex.DefaultController',
					'WebOs.ex.ComCombo','WebOs.ex.PropCombo'
					],

			launch : function() {

				me.configure = Ext.create('resources.Config');
				me.loadTheme();
				Ext.defer(function() {
					Ext.create('Ext.container.Viewport', {
								layout : 'border',
								padding : '0 5 0 5',
								items : [{
											xtype : 'vf_tradition_headerContainer',
											region : 'north'
										}, {
											xtype : 'vf_tradition_naviTreePanel',
											region : 'west',
											viewConfig : {

											}
										}, {
											xtype : 'vf_tradition_contentTabPanel',
											region : 'center'
										}, {
											xtype : 'vf_tradition_statusBar',
											region : 'south'
										}]
							});
					me.tabPanel = Ext.getCmp('vf_tradition_contentTabPanel');
					me.tabManager = Ext.create(
							'WebOs.ViewFrame.tradition.view.TabManager', {
								tabPanel : me.tabPanel
							});
				}, 50);

				core.addListener('repeatstartservice', me.restoreTab);
				Ext.defer(me.load, 50, me);
			}
		});
		me.callParent(arguments);
	},

	load : function() {
		var me = this;
		var store = this.getServiceStore();
		var json = this.getServiceJson();
		// me.startmenu.initData();
		// me.shortcutView.initData();
		core.display('Service_mainPage');
	},
	/**
	 * 加载展示框架对应主题
	 */
	loadTheme : function() {
		var vfname = core.getActiveView().name;
		var str = core.getUserData(Constants.USER_COOKIE_THEME_PREFIX + vfname);
		var theme = this.configure.getThemeCss(str);
		Ext.util.CSS.swapStyleSheet('theme_css', theme['src']);
	},

	/**
	 * 将外部原始service转换为本展示框架所用service store
	 * 
	 * @return {}
	 */
	transformServiceToLocalStore : function() {
		var me = this;
		var serStore = core.getUserServiceStore();
		var localStore = Ext.create('Ext.data.Store', {
					model : 'WebOs.ViewFrame.tradition.model.ServiceModel'
				});
		serStore.each(function(model) {
					a = model;
					var obj = copyModelToObject(model);
					var icons = me.configure.getIcons(model.get('name'));
					obj.largeIcon = icons['large'];
					obj.smallIcon = icons['small'];
					localStore.add(obj);
				});
		this.localStore = localStore;
		return localStore;
	},
	/**
	 * 获取用户业务模块对应的serviceStore
	 * 
	 * @return {}
	 */
	getServiceStore : function() {
		if (this.localStore) {
			return this.localStore;
		} else {
			return this.transformServiceToLocalStore();
		}
	},
	/**
	 * 获取用户业务模块对应的service树状json
	 * 
	 * @return {}
	 */
	getServiceJson : function() {
		if (this.localJson) {
			return this.localJson;
		} else {
			return this.transformServiceToLocalJson();
		}
	},

	/**
	 * 获取用户业务模块对应的service树状json
	 * 
	 * @param {}
	 *            pname
	 * @return {}
	 */
	transformServiceToLocalJson : function(pname) {
		var me = this;
		var nodes = [];
		if (!pname) {
			pname = 'root';
		}
		var children = this.getServiceByParent(pname);
		children.each(function(model) {
					var node = copyModelToObject(model);
					node.expanded = true;
					if (node.serviceType == Constants.SFS_TYPE_TOC) { // 目录模块
						node.leaf = false;
						node.icon = node.smallIcon;
						node.children = me
								.transformServiceToLocalJson(node.name);
					} else if (node.serviceType == Constants.SFS_TYPE_SER) { // 服务模块
						node.leaf = true;
						node.icon = node.smallIcon;
					}
					nodes.push(node);
				});
		return nodes;
	},
	/**
	 * 根据parentName找到对应的业务模块
	 * 
	 * @param {}
	 *            pname
	 * @return {}
	 */
	getServiceByParent : function(pname) {
		if (!pname) {
			return null;
		}
		var store = this.getServiceStore();
		var filtered = store.queryBy(function(record) {
					if (record.get('pname') != pname) {
						return false;
					} else {
						return true;
					}
				});
		return filtered;
	},

	restoreTab : function(service,config) {
		var tab = core.getActiveView().tabManager.getTabByServiceName(service.get('name'));
		if (tab) {
			tab.destroy();
		}
		var viewService = Ext.create(service.get('serviceCls'));
		var ctrlCls = viewService.ctrlCls;
		var controller = this.getActiveView().getApplication().getController(ctrlCls);
		Ext.apply(controller,config);
		core.display(service.get('name'),config);
	},
	showUI : function(viewService) {
		var me = this;
		if (viewService) {
			var viewCls = viewService.viewCls;
			var view = null;
			if (viewCls) {
				view = Ext.create(viewCls);
				var tab = me.tabPanel.add({
							xtype : 'vf_tradition_contentPanel',
							layout : 'fit',
							title : viewService.text,
							serviceName : viewService.name,
							closable : true,
							icon : me.configure.getIcons(viewService.name)['small'],
							items : [view]
						});
				me.tabPanel.setActiveTab(tab);
			}
		}
	},

	closeUI : function(name) {
		var tab = this.tabManager.getTabByServiceName();
		if (tab) {
			tab.destroy();
		}
	}

});